/// Creator:      fengliang
/// CreatDate:    2022-06-14
/// Description:  模板组件
Class CF.NUR.MNISV3.ModelForm Extends %Persistent
{

Relationship ChildRec As CF.NUR.MNISV3.ModelItem [ Cardinality = children, Inverse = RecParref ];

/// Form模板代码
Property FormCode As %Library.String(TRUNCATE = 1) [ Required, SqlColumnNumber = 2 ];

/// Form模板名称
Property FormName As %Library.String [ SqlColumnNumber = 3 ];

/// 顺序
Property FormSort As %Library.String(TRUNCATE = 1) [ SqlColumnNumber = 4 ];

/// 标签是否显示："N"不显示 默认显示
Property LabelFlag As %Library.String(TRUNCATE = 1) [ SqlColumnNumber = 5 ];

/// 精简标记 - 未使用
Property SimpleFlag As %Library.String(TRUNCATE = 1) [ SqlColumnNumber = 6 ];

/// 院区ID 
Property HospId As %String(TRUNCATE = 1) [ SqlColumnNumber = 7 ];

/// Form模板类型: BLOOD 输血, TOUR 巡视 
Property FormType As %String(TRUNCATE = 1) [ SqlColumnNumber = 8 ];

/// Form模板子类型
Property FormSub As %String(TRUNCATE = 1) [ SqlColumnNumber = 9 ];

/// 表单背景色
Property BackgroundColor As %String(TRUNCATE = 1) [ SqlColumnNumber = 10 ];

/// 表单名颜色
Property Color As %String(TRUNCATE = 1) [ SqlColumnNumber = 11 ];

/// 表单名字体大小
Property FontSize As %String(TRUNCATE = 1) [ SqlColumnNumber = 12 ];

/// 居左left 居中center 居右right
Property FormStyle As %String(TRUNCATE = 1) [ SqlColumnNumber = 13 ];

/// 正常normal 加粗bold 斜体italic
Property FontWeight As %String(TRUNCATE = 1) [ SqlColumnNumber = 14 ];

/// 边距：一个值 上下左右；两个值：第一个左右，第二个 上下  默认10
Property Margin As %String(TRUNCATE = 1) [ SqlColumnNumber = 15 ];

/// 索引 院区ID + Form模板代码
Index Code On (HospId, FormCode);

Index FormCode On FormCode;

Index Type On FormType;

/// Creator:      fengliang
/// CreatDate:    2022-06-14
/// Description:  保存
/// other:       w ##class(CF.NUR.MNISV3.ModelForm).Save("","FormCode|PATINFO^FormName|患者信息^FormSort|1^LabelFlag|^SimpleFlag|^HospId|2")
/// other:       w ##class(CF.NUR.MNISV3.ModelForm).Save("","FormCode|BLOODINFO^FormName|血袋信息^FormSort|2^LabelFlag|^SimpleFlag|^HospId|2")
/// other:       w ##class(CF.NUR.MNISV3.ModelForm).Save("","FormCode|GETENTER^FormName|取血输入信息^FormSort|3^LabelFlag|^SimpleFlag|^HospId|2")
/// other:       w ##class(CF.NUR.MNISV3.ModelForm).Save("","FormCode|STARTENTER^FormName|开始输入信息^FormSort|3^LabelFlag|^SimpleFlag|^HospId|2")
/// other:       w ##class(CF.NUR.MNISV3.ModelForm).Save("","FormCode|TOURENTER^FormName|巡视输入信息^FormSort|3^LabelFlag|^SimpleFlag|^HospId|2")
/// other:       w ##class(CF.NUR.MNISV3.ModelForm).Save("","FormCode|ENDENTER^FormName|结束输入信息^FormSort|3^LabelFlag|^SimpleFlag|^HospId|2")
/// other:       w ##class(CF.NUR.MNISV3.ModelForm).Save("","FormCode|RETRIEVEENTER^FormName|回收输入信息^FormSort|3^LabelFlag|^SimpleFlag|^HospId|2")
ClassMethod Save(id, parr)
{
    s tmp=""
    s a=##class(web.DHCMGNurComm).setmoudtmp(parr,.tmp)
    s FormCode=$zcvt(tmp("FormCode"),"u")
    s rw=$O(^CF.NUR.MNISV3.ModelFormI("FormCode"," "_FormCode,""))
    i rw'="" s id=rw //更新数据
    if id'=""
    {
        s rec=##class(CF.NUR.MNISV3.ModelForm).%OpenId(id)
    }else{
        s rec=##class(CF.NUR.MNISV3.ModelForm).%New()
    }
    
    i $d(tmp("FormCode")) s rec.FormCode=tmp("FormCode")
    i $d(tmp("FormName")) s rec.FormName=tmp("FormName")
    i $d(tmp("FormSort")) s rec.FormSort=tmp("FormSort")
    i $d(tmp("FormType")) s rec.FormType=tmp("FormType") 
    i $d(tmp("LabelFlag")) s rec.LabelFlag=tmp("LabelFlag")
    i $d(tmp("SimpleFlag")) s rec.SimpleFlag=tmp("SimpleFlag")
    i $d(tmp("HospId")) s rec.HospId=tmp("HospId")  
    i $d(tmp("FormSub")) s rec.FormSub=tmp("FormSub") 
    s sc=rec.%Save()
    s id=rec.%Id()
    d rec.%Close()
    q:$$$ISERR(sc) "err:"_$SYSTEM.Status.GetErrorText(sc)
    q id
}

///  w ##class(CF.NUR.MNISV3.ModelForm).GetModelFormsByType()
ClassMethod GetModelFormsByType(type = "TOUR")
{
    s rw=""
    f
    {   //遍历 输血类型节点 sort排序
        s rw=$o(^CF.NUR.MNISV3.ModelFormI("Type"," "_type,rw))
        q:rw=""
        w rw,!
    }
}

/// 通过模板代码获取模板信息--- 支持多个modelCode 用"!"分割
/// w ##class(Nur.JSON).Encode(##class(CF.NUR.MNISV3.ModelForm).GetModelInfoByModeCode("DHCNurGradeTourPda","2"))
/// w ##class(Nur.JSON).Encode(##class(CF.NUR.MNISV3.ModelForm).GetModelInfoByModeCode("PATINFO!DHCNurGradeTourPda","2"))
ClassMethod GetModelInfoByModeCode(modeCode, hospitalId)
{
    s ^GRS("GetModelInfoByModeCode11")=$lb(modeCode,hospitalId)
    s formList=##class(%ListOfDataTypes).%New() 
    s subId=""
    f i=1:1:$l(modeCode,"!") //遍历可操作节点
    {
        s formCode=$p(modeCode,"!",i)
        continue:formCode=""
        s formCode=$ZCVT(formCode,"U") //Form组件索引
        s formId=$o(^CF.NUR.MNISV3.ModelFormI("Code"," "_hospitalId," "_formCode,""))
        continue:formId=""
        s formData=..GetModelInfoSingleByModelCode(formCode,hospitalId)
        d formList.Insert(formData) //插入form
    
    }
    s modelInfo=##class(%ArrayOfDataTypes).%New()
    d modelInfo.SetAt($g(formList),"formList")
    q modelInfo
}

/// *****************通过模板代码获取模板信息*****************
/// w ##class(Nur.JSON).Encode(##class(CF.NUR.MNISV3.ModelForm).GetModelInfoSingleByModelCode("PATINFO","2"))
/// w ##class(Nur.JSON).Encode(##class(CF.NUR.MNISV3.ModelForm).GetModelInfoSingleByModelCode("BLOODINFO","2"))
ClassMethod GetModelInfoSingleByModelCode(formCode, hospitalId)
{
    s ^GRS("GetModelInfoSingleByModelCode")=$lb(formCode,hospitalId)
    s formData=##class(%ArrayOfDataTypes).%New()
    s formCode=$ZCVT(formCode,"U") //Form组件索引
    s formId=$o(^CF.NUR.MNISV3.ModelFormI("Code"," "_hospitalId," "_formCode,""))
    q:formId="" formData
    //w "formId="_formId,!
    s itemList=##class(%ListOfDataTypes).%New() 
    s itemId=""
    f 
    {
        s itemId=$o(^CF.NUR.MNISV3.ModelItemD(formId,itemId)) //遍历Item配置
        q:itemId=""
        //w "itemId="_formId_"||"_itemId,!
        s itemObj=##class(CF.NUR.MNISV3.ModelItem).%OpenId(formId_"||"_itemId) 
        s position=itemObj.Position
        s itemData=##class(%ArrayOfDataTypes).%New()
        d itemData.SetAt(itemObj.ItemName,"ItemName")
        d itemData.SetAt(itemObj.ItemCode,"ItemCode")
        i itemObj.ItemType'="" d itemData.SetAt(itemObj.ItemType,"ItemType")
        i itemObj.ItemNote'="" d itemData.SetAt(itemObj.ItemNote,"ItemNote")
        i itemObj.ItemSort'="" d itemData.SetAt(itemObj.ItemSort,"ItemSort")
        //位置 1,2 是第一行 第二列
        i itemObj.Position'="" d itemData.SetAt(itemObj.Position,"Position")
        i itemObj.LinkCode'="" d itemData.SetAt(itemObj.LinkCode,"LinkCode")
        i itemObj.LabelFlag'="" d itemData.SetAt(itemObj.LabelFlag,"LabelFlag")
        i itemObj.SignCode'="" d itemData.SetAt(itemObj.SignCode,"SignCode")
        i itemObj.RequiredFlag'="" {
            s requiredStar="<font color=""red"">*</font>"
            i itemObj.RequiredFlag="Y" d itemData.SetAt(itemObj.ItemName_requiredStar,"ItemName")
            d itemData.SetAt(itemObj.RequiredFlag,"RequiredFlag")
        }
        //对于Android类型: 文本TYPE_CLASS_TEXT=1; 数字TYPE_CLASS_NUMBER=2; TYPE_NUMBER_FLAG_DECIMAL=2000
        i itemObj.KeyboardType'="" d itemData.SetAt(itemObj.KeyboardType,"KeyboardType")
        i itemObj.SimpleFlag'="" d itemData.SetAt(itemObj.SimpleFlag,"SimpleFlag")
        i itemObj.DataSource'="" d itemData.SetAt(itemObj.DataSource,"DataSource")
        i itemObj.ItemOption'="" d itemData.SetAt(itemObj.ItemOption,"ItemOption")
        i itemObj.ItemDefault'="" d itemData.SetAt(itemObj.ItemDefault,"ItemDefault")
        //是否能编辑  
        //s disabledSpan="<span style=""color:gray"">",spanEnd="</span>"
        i itemObj.CanEditFlag'="" d itemData.SetAt(itemObj.CanEditFlag,"CanEditFlag")
        /////////////////////////////////新增的字段/////////////////////////////////
        /// 占屏幕百分比 - item的宽度；取值0~1 优先宽度，不设置则根据Position平分一行中的item
        i itemObj.ItemWidth'="" d itemData.SetAt(itemObj.ItemWidth,"ItemWidth")
        /// item的高度：取值大于1 一般50即可
        i itemObj.ItemHeight'="" d itemData.SetAt(itemObj.ItemHeight,"ItemHeight")
        /// item的行高
        i itemObj.LineHeight'="" d itemData.SetAt(itemObj.LineHeight,"LineHeight")
        /// 值Y：简单签名布局--输入回车调用editTextConvert后台
        i itemObj.SignFlag'="" d itemData.SetAt(itemObj.SignFlag,"SignFlag")
        /// item的颜色 <custom>标签
        i itemObj.Color'="" d itemData.SetAt(itemObj.Color,"Color")
        /// item的字体 <custom>标签
        i itemObj.FontSize'="" d itemData.SetAt(itemObj.FontSize,"FontSize")
        /// item字体样式：bold <custom>标签
        i itemObj.FontWeight'="" d itemData.SetAt(itemObj.FontWeight,"FontWeight")
        /// ItemName标签颜色  <custom>标签
        i itemObj.LabelColor'="" d itemData.SetAt(itemObj.LabelColor,"LabelColor")
        /// ItemName标签字体大小  <custom>标签
        i itemObj.LabelFontSize'="" d itemData.SetAt(itemObj.LabelFontSize,"LabelFontSize")
        /// ItemName标签样式：bold <custom>标签
        i itemObj.LabelFontWeight'="" d itemData.SetAt(itemObj.LabelFontWeight,"LabelFontWeight")
        /// 背景(包含item内容，ItemName标签，layoutChild背景)： <custom>标签
        i itemObj.BackgroundColor'="" d itemData.SetAt(itemObj.BackgroundColor,"BackgroundColor")
        /// 边距：一个值 上下左右；两个值：第一个左右，第二个 上下
        i itemObj.Margin'="" d itemData.SetAt(itemObj.Margin,"Margin")
        /// 位置居中 居右
        i itemObj.Gravity'="" d itemData.SetAt(itemObj.Gravity,"Gravity")
        /// 关联 对应code 例如"RelateIds": "有=Symptom!SymptomElse!Measure" R是radio按钮单选，对应RelateIds关联‘有’，则显示ItemCode=‘Symptom‘等，点击‘无’则隐藏
        i itemObj.RelateIds'="" d itemData.SetAt(itemObj.RelateIds,"RelateIds")
        /// 是否隐藏- 1或Y是
        i itemObj.RelateHide'="" d itemData.SetAt(itemObj.RelateHide,"RelateHide")
        d itemData.SetAt(formId_"||"_itemId,"itemId")             
        d itemObj.%Close()
        d itemList.Insert(itemData) //插入items
    }
    s fromObj=##class(CF.NUR.MNISV3.ModelForm).%OpenId(formId)
    s FormCode=fromObj.FormCode
    s FormName=fromObj.FormName
    s FormSort=fromObj.FormSort
    s FormType=fromObj.FormType
    s LabelFlag=fromObj.LabelFlag
    s SimpleFlag=fromObj.SimpleFlag 
    d fromObj.%Close()
    d formData.SetAt($g(itemList),"itemList")
    d formData.SetAt($g(FormCode),"FormCode")
    d formData.SetAt($g(FormName),"FormName")
    d formData.SetAt($g(formId),"ID")
    //显示类型
    i ($g(FormCode)="PATINFO")||($g(FormCode)="BLOODINFO") s FormType="1"
    i $g(FormName)["医嘱信息" s FormType="M"
    d formData.SetAt($g(FormType),"FormType")
    d formData.SetAt($g(LabelFlag),"LabelFlag")
    d formData.SetAt($g(SimpleFlag),"SimpleFlag")
    q formData
}

/// 通过模板代码获取模板信息---并赋值默认值
ClassMethod GetModelInfoSingleWithObject(formCode, hospitalId, bloodInfo)
{
    s modelObj=..GetModelInfoSingleByModelCode(formCode,hospitalId)
    s itemList=modelObj.GetAt("itemList")
    f index=1:1:itemList.Count(){
        s item=itemList.GetAt(index)
        s key=item.GetAt("DataSource")
        i key'="" s val=bloodInfo.GetAt(key)
        i $g(val)'="" d item.SetAt(val,"ItemDefault")
    }  
    q modelObj
}

/// 通过模板代码获取模板信息并赋值默认值---再加入List中
ClassMethod GetModelInfoSingleWithObjectToList(formCode, hospitalId, bloodInfo)
{
    s modelObj=..GetModelInfoSingleWithObject(formCode,hospitalId,bloodInfo)
    s modelList=##class(%ListOfDataTypes).%New()
    d modelList.Insert(modelObj)
    q modelList
}

/// 获取当前对象%ArrayOfDataTypes
/// w ##class(Nur.JSON).Encode(##class(CF.NUR.MNISV3.ModelForm).GetVal(1))
ClassMethod GetVal(id) As %ArrayOfDataTypes
{
    s ret=""
    s obj=##class(%ArrayOfDataTypes).%New()
    s className = $CLASSNAME() ;获取当前类名
    if id'=""
    {   
        s a=..%OpenId(id) ;打开当前对象
        s tdata="",tmp=""
        s p="" 
        f  
        {
            s p=$O(^oddCOM(className,"a",p))
            q:p="" 
            continue:p["%"
            s typ=^oddCOM(className,"a",p,40)
            s tmp(p)=$ZOBJPROPERTY(a,p)
            //处理日期 时间
            i (typ="%Library.Date")&&(tmp(p)'="") s tmp(p)=$ZD(tmp(p),3)
            i (typ="%Library.Time")&&(tmp(p)'="") s tmp(p)=$ZT(tmp(p))
            //处理用户Id-->姓名
            i (p["User")&&(tmp(p)'="") { 
                i $d(^SSU("SSUSR",tmp(p))) s tmp(p)=$P($G(^SSU("SSUSR",tmp(p))),"^",2)
            }
            d obj.SetAt(tmp(p),p)
        }
    }
    q obj
}

ClassMethod GetUser(User) As %String
{
      S User=$G(User)
      S (user,SSName)=""
      I '$L(User) Q user
      Q:'$D(^SSU("SSUSR",User)) user
      I $D(^SSU("SSUSR",User)) s SSName=$P($G(^SSU("SSUSR",User)),"^",2)
      Q SSName
}

Storage Default
{
<Data name="ModelFormDefaultData">
<Value name="1">
<Value>%%CLASSNAME</Value>
</Value>
<Value name="2">
<Value>FormCode</Value>
</Value>
<Value name="3">
<Value>FormName</Value>
</Value>
<Value name="4">
<Value>FormSort</Value>
</Value>
<Value name="5">
<Value>LabelFlag</Value>
</Value>
<Value name="6">
<Value>SimpleFlag</Value>
</Value>
<Value name="7">
<Value>HospId</Value>
</Value>
<Value name="8">
<Value>FormType</Value>
</Value>
<Value name="9">
<Value>FormSub</Value>
</Value>
<Value name="10">
<Value>BackgroundColor</Value>
</Value>
<Value name="11">
<Value>Color</Value>
</Value>
<Value name="12">
<Value>FontSize</Value>
</Value>
<Value name="13">
<Value>FormStyle</Value>
</Value>
<Value name="14">
<Value>FontWeight</Value>
</Value>
<Value name="15">
<Value>Margin</Value>
</Value>
</Data>
<DataLocation>^CF.NUR.MNISV3.ModelFormD</DataLocation>
<DefaultData>ModelFormDefaultData</DefaultData>
<IdLocation>^CF.NUR.MNISV3.ModelFormD</IdLocation>
<IndexLocation>^CF.NUR.MNISV3.ModelFormI</IndexLocation>
<StreamLocation>^CF.NUR.MNISV3.ModelFormS</StreamLocation>
<Type>%Storage.Persistent</Type>
}

}
